package com.yanq.liancar.fragment;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.BLoginActivity;
import com.yanq.liancar.activity.MailAddressActivity;
import com.yanq.liancar.activity.SelectActivity;
import com.yanq.liancar.activity.SmallBMySetActivity;
import com.yanq.liancar.activity.SmallCNewsActivity;
import com.yanq.liancar.activity.SmallCPersonalInformation;
import com.yanq.liancar.base.BaseLazyFragment;
import com.yanq.liancar.bean.QueryUserBaseMessageBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.LogUtils;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class BMineFragment extends BaseLazyFragment {
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.my_img_service)
    ImageView myImgService;
    @BindView(R.id.my_img_message)
    ImageView myImgMessage;
    @BindView(R.id.my_img_set)
    ImageView myImgSet;
    @BindView(R.id.tool_bar)
    RelativeLayout toolBar;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.tv_sgin)
    TextView tvSgin;
    @BindView(R.id.user_data_ll)
    LinearLayout userDataLl;
    @BindView(R.id.next)
    ImageView next;
    @BindView(R.id.rl_top)
    LinearLayout rlTop;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.headimg)
    CircleImageView headimg;

    public static BMineFragment newInstance(String param1) {
        BMineFragment fragment = new BMineFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogUtils.e("111111111111","BMineFragment======onViewCreated");
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_bmine;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
            login.setVisibility(View.VISIBLE);
            userDataLl.setVisibility(View.GONE);
        } else {
            login.setVisibility(View.GONE);
            userDataLl.setVisibility(View.VISIBLE);
            initOkGo();
        }

        LogUtils.e("111111111111","BMineFragment======onResume");
    }

    @Override
    protected void initData() {
        super.initData();
        LogUtils.e("111111111111","BMineFragment======initData");
    }

    @Override
    protected void initView() {
        super.initView();
        LogUtils.e("111111111111","BMineFragment======initView");

    }

    @Override
    protected void setListener() {
        super.setListener();
        LogUtils.e("111111111111","BMineFragment======setListener");
    }

    @OnClick({R.id.my_img_service, R.id.my_img_message, R.id.my_img_set, R.id.login,R.id.tv_address,R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_img_service:
                //H5Activity.start(mActivity, "https://chat.kuaishangkf.com/#/?script=T4zEN8t53kAXTSGNrdPHBNbD");
//                String title = "智能客服";
//                ConsultSource source = new ConsultSource("", "我的", "custom information string");
//                Unicorn.openServiceActivity(mActivity, title, source);
                Intent intent0 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4008055011"));
                intent0.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent0);
                break;
            case R.id.my_img_message:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("where", "BHomeFragment");
                    RxActivityTool.skipActivity(mActivity, SmallCNewsActivity.class, bundle1);
                }
                break;
            case R.id.login:
                Intent intent = new Intent(getContext(), BLoginActivity.class);
                startActivity(intent);
                break;
            case R.id.my_img_set:
                if (!TextUtils.isEmpty(UserBean.getbToken(mActivity))){
                    Bundle bundle=new Bundle();
                    bundle.putInt("where",2);
                    RxActivityTool.skipActivity(mActivity, SmallBMySetActivity.class,bundle);
                }else {
                    RxActivityTool.skipActivity(mActivity, SelectActivity.class);
                }

                break;
            case R.id.tv_address:
                if (!TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    Bundle bundle1 =new Bundle();
                    bundle1.putString("where","BMineFragment");
                    RxActivityTool.skipActivity(mActivity, MailAddressActivity.class,bundle1);
                } else {
                    Toast.makeText(getContext(), "请先登陆", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.next:
                Bundle bundle1=new Bundle();
                bundle1.putString("where","BMineFragment");
                RxActivityTool.skipActivity(mActivity, SmallCPersonalInformation.class,bundle1);
                break;

        }
    }
    private void initOkGo() {
        OkGo.<String>post(Config.s + "llc/queryUserBaseMessage")
                .tag(this)
                .params("token",UserBean.getbToken(mActivity))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        QueryUserBaseMessageBean bean = gson.fromJson(response.body(), QueryUserBaseMessageBean.class);
                        if (bean.isIsSuccess()) {
                            RequestOptions options=new RequestOptions();
                            options.error(R.mipmap.vip_touxiang);
                            Glide.with(mActivity).load(bean.getData().getHeadPage()).apply(options).into(headimg);
                            userName.setText(bean.getData().getNickname());
                            tvSgin.setText(bean.getData().getSign());
                        } else {
                            RxToast.error(bean.getOutputMessage());
                            login.setVisibility(View.VISIBLE);
                            userDataLl.setVisibility(View.GONE);
                        }
                    }
                });
    }


}
